package _18_剑指OfferII;

import _18_剑指OfferII.datastrcute.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class _055_剑指OfferII二叉搜索树迭代器 {

    private int index;

    private List<TreeNode> containers;

    // 扁平化二叉树， 可以直接在节点修改
    public _055_剑指OfferII二叉搜索树迭代器(TreeNode root) {
        containers = new ArrayList<>();

        traverseTree(root);
    }


    public int next() {
        return containers.get(index++).val;
    }

    public boolean hasNext() {
        return index < containers.size();
    }

    private void traverseTree(TreeNode root) {
        if (root == null) return;

        traverseTree(root.left);
        containers.add(root);
        traverseTree(root.right);
    }


}
